package com.rattsoftware.server.serviceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.annotation.Secured;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rattsoftware.server.admin.model.Permission;
import com.rattsoftware.server.admin.model.Role;
import com.rattsoftware.server.admin.model.User;
import com.rattsoftware.server.admin.model.paging.PagingLoadConfig;
import com.rattsoftware.server.admin.model.paging.PagingLoadResult;
import com.rattsoftware.server.dao.PermissionDAO;
import com.rattsoftware.server.dao.RoleDAO;
import com.rattsoftware.server.dao.UserDAO;
import com.rattsoftware.server.service.RoleService;

/**
 * Default implementation for <code>Permission Service</code>.
 * 
 *
 */
@Service("roleService")
@Transactional
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleDAO roleDAO;
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private PermissionDAO permissionDAO;

	public void setRoleDAO(RoleDAO roleDAO) {
		this.roleDAO = roleDAO;
	}

	@Override
	@Secured("Perm_Delete_Roles")
	public void deleteRole(Role role) {
		roleDAO.deleteRole(role);
	}

	@Override
	@Transactional(readOnly = true)
	@Secured("Perm_View_Roles")
	public List<Role> getRoles() {
		return roleDAO.getRoles();
	}

	@Override
	@Secured("Perm_View_Roles")
	public List<Role> getRolesByName(String name) {
		return roleDAO.getRolesByName(name);
	}

	@Override
	@Secured("Perm_Add_Roles")
	public void saveRole(Role role) {
		roleDAO.saveRole(role);
	}

	@Override
	@Transactional(readOnly = true)
	// note: no security because it is used during authentication
	public List<Permission> getPermissions() {
		return permissionDAO.getPermissions();
	}

	@Override
	@Secured("Perm_Delete_Permissions")
	public void deletePermission(Permission permission) {
		permissionDAO.deletePermission(permission);
	}

	@Override
	@Secured("Perm_Add_Permissions")
	public void savePermission(Permission permission) {
		permissionDAO.savePermission(permission);
	}

	@Override
	@Transactional(readOnly = true)
	public Permission getPermission(String permissionName) {
		return permissionDAO.getPermission(permissionName);
	}

	@Override
	@Secured("Perm_View_Roles")
	public PagingLoadResult<Role> getMappedRoles(String userId, PagingLoadConfig pagingLoadConfig) {
		return roleDAO.getMappedRoles(userId, pagingLoadConfig);
	}

	@Override
	@Secured("Perm_View_Roles")
	public PagingLoadResult<Role> getUnMappedRoles(String userId, PagingLoadConfig pagingLoadConfig) {
		return roleDAO.getUnMappedRoles(userId, pagingLoadConfig);
	}

	@Override
	@Secured("Perm_Add_Users")
	public void saveMappedRoles(String userId, List<Role> rolesToAdd, List<Role> rolesToDelete) {
		User user = userDAO.getUser(userId);
		if (rolesToAdd != null) {
			for (Role r : rolesToAdd) {
				user.getRoles().add(r);
			}
		}
		if (rolesToDelete != null) {
			for (Role r : rolesToDelete) {
				user.getRoles().remove(r);
			}
		}
		userDAO.save(user);
	}
}
